package com.miku.blog.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
import java.util.Date;
import java.util.UUID;

/**
 * @author shkstart
 * @create ${}YEAR-05-10 18:12
 */
public class JwtUtils {

    private static final String JWT_KEY = "x5/m";
    private static final Long JWT_TIME = 60 * 60 * 1000L; // 毫秒 [一小时]
    private static final String JWT_ISSUER = "miku";

    public static String getUUID(){
        return UUID.randomUUID().toString().replaceAll("-","");
    }

    //返回token
    public static String creatJwt(String subject){
        JwtBuilder jwtBuilder = getJwtBuilder(subject, null, getUUID());
        return jwtBuilder.compact();
    }
    public static String creatJwt(String subject,Long timeMillis){
        JwtBuilder jwtBuilder = getJwtBuilder(subject, timeMillis, getUUID());
        return jwtBuilder.compact();
    }
    public static String creatJwt(String subject,Long timeMillis,String id){
        JwtBuilder jwtBuilder = getJwtBuilder(subject, timeMillis, id);
        return jwtBuilder.compact();
    }

    public static String creatJwt(String subject,Date startTime,Date endTime,String id){
        JwtBuilder jwtBuilder = getJwtBuilder(subject, startTime, endTime, id);
        return jwtBuilder.compact();
    }

    public static JwtBuilder getJwtBuilder(String subject,Long timeMillis,String id){
        long startTimeMillis = System.currentTimeMillis();
        Date startDate = new Date(startTimeMillis);
        if (timeMillis == null){
            timeMillis = JWT_TIME;
        }
        long expireTimeMillis = startTimeMillis + timeMillis;
        Date expireDate = new Date(expireTimeMillis);

        return getJwtBuilder(subject,startDate,expireDate,id);

//        return Jwts.builder()
//                .signWith(SignatureAlgorithm.HS256,generalKey()) // 使用 HS256签名算法 及 密钥 生成JWT
//                .setId(id)
//                .setSubject(subject)
//                .setIssuedAt(startDate)
//                .setIssuer(JWT_ISSUER)
//                .setExpiration(expireDate);
    }


    public static JwtBuilder getJwtBuilder(String subject,Date startTime,Date endTime,String id){

        return Jwts.builder()
                .signWith(SignatureAlgorithm.HS256, generalKey())
                .setId(id)
                .setSubject(subject)
                .setIssuedAt(startTime)
                .setIssuer(JWT_ISSUER)
                .setExpiration(endTime);
    }

    public static Claims parseJwt(String token){
        return Jwts.parser()
                .setSigningKey(generalKey())
                .parseClaimsJws(token)
                .getBody();
    }

    public static SecretKey generalKey(){
        byte[] encodeKey = Base64.getDecoder().decode(JWT_KEY);
        SecretKeySpec secretKeySpec = new SecretKeySpec(encodeKey,0,encodeKey.length,"AES"); // AES对称加密算法
        return secretKeySpec;
    }


}
